/*
 *
 *
 */

#include "Text.h"

#ifndef _TEXT_CPP_
#define _TEXT_CPP_

/*
 * os parametros são:
 * text             texto a ser escrito
 * fontFile         arquivo .ttf da fonte utilizada
 * x,y              posição x y da superficie do texto na tela
 * style            estilo da fonte exemplo TTF_STYLE_NORMAL ou TTF_STYLE_BOLD|TTF_STYLE_ITALIC
 * red green blue   cores rgb de 0 a 255
 * ptsize           tamanho da fonte
 */ 
Text::Text(string text, string fontFile,double x, double y,int style, Uint8 red, Uint8 green, Uint8 blue, int ptsize)
{
    this->fontFile.assign(fontFile);
    if(!(font = TTF_OpenFont(this->fontFile.c_str(),ptsize)))
        fprintf(stderr,"Erro ao carregar arquivo de fonte %s\n%s\n",fontFile.c_str(),TTF_GetError());
    this->text.assign(text);
    this->x = x;
    this->y = y;
    this->style = style;
    TTF_SetFontStyle(font, this->style);
    color.r = red;
    color.g = green;
    color.b = blue;
    if(!(textSurface = TTF_RenderText_Blended(font, this->text.c_str(), color)))
        fprintf(stderr,"Erro ao carregar superficie da fonte %s\n%s\n",fontFile.c_str(),TTF_GetError());
    else
    {
        rect.x = (int) x;
        rect.y = (int) y;
        rect.w = textSurface->w;
        rect.h = textSurface->h;
    }
}

/*
 * 
 */ 
Text::~Text(void)
{
    if(font)
        TTF_CloseFont(font);
    font = 0;
    if(textSurface)
        SDL_FreeSurface(textSurface);
    textSurface = 0;
}

/*
 * 
 */ 
void Text::render(SDL_Surface *screen)
{
    rect.x = (int) x;
    rect.y = (int) y;
    SDL_BlitSurface(textSurface, NULL, screen, &rect); 
}

/*
 * 
 */
void Text::updatePos(double x, double y)
{
    this->x = x;
    this->y = y;
}

/*
 * 
 */ 
void Text::changeText(string text)
{
    if(textSurface)
    {
        SDL_FreeSurface(textSurface);
        textSurface = 0;
    }
    this->text.assign(text);
    if(!(textSurface = TTF_RenderText_Blended(font, this->text.c_str(), color)))
        fprintf(stderr,"Erro ao carregar superficie da fonte %s\n%s\n",fontFile.c_str(),TTF_GetError());
    else
    {
        rect.w = textSurface->w;
        rect.h = textSurface->h;
    }
}

/*
 * 
 */ 
void Text::changeStyle(int style)
{
    this->style = style;
    if(textSurface)
    {
        SDL_FreeSurface(textSurface);
        textSurface = 0;
    }
    TTF_SetFontStyle(font, this->style);
    if(!(textSurface = TTF_RenderText_Blended(font, this->text.c_str(), color)))
        fprintf(stderr,"Erro ao carregar superficie da fonte %s\n%s\n",fontFile.c_str(),TTF_GetError());
    else
    {
        rect.w = textSurface->w;
        rect.h = textSurface->h;
    }
}

/*
 * 
 */ 
void Text::changeColor(int red, int green, int blue)
{
    color.r = red;
    color.g = green;
    color.b = blue;
    if(textSurface)
    {
        SDL_FreeSurface(textSurface);
        textSurface = 0;
    }
    TTF_SetFontStyle(font, this->style);
    if(!(textSurface = TTF_RenderText_Blended(font, this->text.c_str(), color)))
        fprintf(stderr,"Erro ao carregar superficie da fonte %s\n%s\n",fontFile.c_str(),TTF_GetError());
    else
    {
        rect.w = textSurface->w;
        rect.h = textSurface->h;
    }
}

/*
 * 
 */ 
void Text::changeSize(int ptsize)
{
    if(textSurface)
    {
        SDL_FreeSurface(textSurface);
        textSurface = 0;
    }
    if(font)
    {
        TTF_CloseFont(font);
        font = 0;
    }
    if(!(font = TTF_OpenFont(fontFile.c_str(),ptsize)))
        fprintf(stderr,"Erro ao carregar arquivo de fonte %s\n%s\n",fontFile.c_str(),TTF_GetError());
    if(!(textSurface = TTF_RenderText_Blended(font, this->text.c_str(), color)))
        fprintf(stderr,"Erro ao carregar superficie da fonte %s\n%s\n",fontFile.c_str(),TTF_GetError());
    else
    {
        rect.w = textSurface->w;
        rect.h = textSurface->h;
    }
}

/*
 * 
 */ 
SDL_Rect Text::getRect(void)
{
    rect.x = (int) x;
    rect.y = (int) y;
    return rect;
}

#endif
